<?php

class MessagesController extends Zend_Controller_Action
{

    public function init()
    {
        try{
    		$session = Zend_Registry::get('session');
    		
    		if($session->user['Username'] != ""){ //ako ne e prazna sesijata znaci deka korisnikot e logiran.
    			
    		}        
        	else{        		
    			$this->_helper->redirector('index','login'); //vo sprotivno vrati go na stranata za logiranje.
    		} 
        
        }
        catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";  
        
        }
    }

    public function indexAction()
    {
        $session = Zend_Registry::get('session');
    	$id_receiver = $session->user['id'];   	//go zemame ID-to na korisnikot koj e momentalno logiran za da mu gi izlistame porakite koi se nameneti za nego		
    	
    	//kreirame objekti od db klasite koi ni se potrebni.
    	$messages = new Application_Model_DbTable_Messages();
        $users = new Application_Model_DbTable_Users();
        $msg_users = new Application_Model_DbTable_Messagesusers();
        
        //gi prosleduvame objektite kon View klasite.
        $this->view->msgs = $messages;
        $this->view->users = $users;
        $this->view->mu = $msg_users->getReceivedMessages($id_receiver);
        
    if ($this->getRequest()->isPost()) {
        	
        	if (isset($_POST['btn_DeleteInboxMsg'])){
		    		$id = $this->getRequest()->getParam("hdn_InboxMsgCurrentDelete");
		    		$msg_users->deleteInboxMessage($id);
		    		$this->_helper->redirector('index');
        	}
        }
    }

    public function viewmsgAction()
    {
        $msg_id = $this->getRequest()->getParam('msg_id');
        $message = new Application_Model_DbTable_Messages();
        $this->view->message = $message->getMessage($msg_id);
    }

    public function createnewAction()
    {
        $form = new Application_Form_Sendmsg();
        $form->submit->setLabel('Send');
        $this->view->form = $form;
        
    	if ($this->getRequest()->isPost()) {    // ako e kliknato na submit kopceto
			$formData = $this->getRequest()->getPost();
			
			if ($form->isValid($formData)) {     // se proveruva dali se popolneti site polinja.
				$title = $form->getValue('title');
				$text = $form->getValue('text');
				$sendto = $form->getValue('sendto');
				
				$message = new Application_Model_DbTable_Messages(); 
				$msg_user = new Application_Model_DbTable_Messagesusers();
				$user = new Application_Model_DbTable_Users();
				
				$msgID = $message->createMessage($title, $text);	// i se povikuva funkcija od model i se dodava stavka vo bazata
				$this->view->lastID = $msgID;
				$session = Zend_Registry::get('session');
 				$sender = $session->user['id'];
				$receiver = $user->getUserID($sendto);
 				
				$msg_user->sendMessage($msgID, $sender, $receiver);
 				
				$this->_helper->redirector('index');
			} 
			else {							//ako ne se, togash se vrakja se vo sostojba koja bila pred kliknuvanje ka kopceto 
				$form->populate($formData);		//If the form data is not valid, then we populate the form with the data that the user filled in and redisplay.
			}
    	}
		
    	
    }

    public function deleteAction()
    {
        $msg_userID = $this->getRequest()->getParam('msg_user_id');  // se zema parametarot koj e praten preku requests so klik na linkot Delete vo index view
        $msg_user = new Application_Model_DbTable_Messagesusers(); 	//se kreira objekt od model klasa
        $msg_user->deleteMessage($msg_userID);						// se povikuva funcija za brishenje nad toj objekt
        $this->_helper->redirector('index');						//vednash redirektirame kon idex view(akcija), bidejkji delete view-to nema html kod.
    }

    public function sentmsgAction()
    {
        $session = Zend_Registry::get('session');
    	$user_id = $session->user['id'];   	//go zemame ID-to na korisnikot koj e momentalno logiran za da mu gi pronajdeme porakite koi toj gi ispratil		
    	
    	//kreirame objekti od db klasite koi ni se potrebni.
    	$messages = new Application_Model_DbTable_Messages();
        $users = new Application_Model_DbTable_Users();
        $msg_users = new Application_Model_DbTable_Messagesusers();
        
        //gi prosleduvame objektite kon View klasite.
        $this->view->msgs = $messages;
        $this->view->users = $users;
        $this->view->mu = $msg_users->getSentMessages($user_id);
        
        if ($this->getRequest()->isPost()) {
        	
        	if (isset($_POST['btn_DeleteSentMsg'])){
		    		$id = $this->getRequest()->getParam("hdn_SentMsgCurrentDelete");
		    		$msg_users->deleteOutboxMessage($id);
		    		$this->_helper->redirector('sentmsg');
        	}
        }
    }

}

